package com.ruoyi.web.sso.controller;



import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.domain.*;
import com.ruoyi.web.sso.domain.vo.AreaVO;
import com.ruoyi.web.sso.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-10-21
 */
@Controller
@RequestMapping("/sso/")
@Api(tags = "页面")
public class ViewController {


    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private  IAreaService areaService;

    @Autowired
    private IPlatformService platformService;

    @Autowired
    private IStaffService staffService;

    @Autowired
    private  IRedisUtilService redisUtilService;

    @Autowired
    private  IAreaStaffService areaStaffService;

    @GetMapping("role/role")
    @ApiOperation(value="系统角色页面",notes="系统角色页面")
    public String menu(ModelMap mmap, @ApiParam(value = "系统ID") @RequestParam String platformId)
    {
        mmap.put("platformId",platformId);
        return "sso/role/role";
    }

    @GetMapping("role/add")
    @ApiOperation(value="新增角色页面",notes="新增角色页面")
    public String add( ModelMap mmap)
    {
        mmap.put("platforms", platformService.selectPlatformList(new Platform()));
        return "sso/role/add";
    }


    @GetMapping("role/authUser")
    @ApiOperation(value="分配用户页面",notes="分配用户页面")
    public String authUser( String roleId, ModelMap mmap)
    {
        mmap.put("role", roleService.selectRoleByRoleId(roleId));
        return   "sso/role/authUser";
    }
    /**
     * 修改角色
     */
    @GetMapping("role/edit")
    @ApiOperation(value="修改角色页面",notes="修改角色页面")
    public String editRole( String roleId, ModelMap mmap)
    {
        mmap.put("platforms", platformService.selectPlatformList(new Platform()));
        mmap.put("role", roleService.selectRoleByRoleId(roleId));
        return  "sso/role/edit";
    }

    @GetMapping("role/selectUser")
    @ApiOperation(value="分配用户页面",notes="分配用户页面")
    public String selectUser(@ApiParam(value = "角色ID")@RequestParam(required=false) String roleId, @ApiParam(value = "系统ID")@RequestParam(required=false) String platformId,ModelMap mmap)
    {
        mmap.put("role", roleService.selectRoleByRoleId(roleId));
        mmap.put("platformId", platformId);
        return   "sso/role/selectUser";
    }


    /**
     * 修改用户信息
     */
    @GetMapping("role/addAreaRole/{id}")
    public String addAreaRole(@PathVariable("id") String id, ModelMap mmap)
    {
        List<Role> roles = roleService.selectRoleList(new Role());
        AreaVO area = areaService.selectAreaById(id);
        mmap.put("roles", roles);
        mmap.put("id", id);
        mmap.put("name", area.getAreaName());
        return  "sso/area/addAreaRole";
    }


//---------------------------------------role-----------------------------------------------------------------------------------
//---------------------------------------分割线-----------------------------------------------------------------------------------
//---------------------------------------staff-----------------------------------------------------------------------------------


    @GetMapping("staff/login")
    @ApiOperation(value="登录页面",notes="登录页面")
    public String login()
    {
        return "login";
    }

    @GetMapping("staff/add")
    @ApiOperation(value="新增用户页面",notes="新增用户页面")
    public String addStaff()
    {
        return "sso/staff/add";
    }

    @GetMapping("staff/list")
    @ApiOperation(value="用户列表页面",notes="用户列表页面")
    public String list()
    {
        return "sso/staff/staff";
    }

    @GetMapping("staff/selectStaffTicket")
    @ApiOperation(value="用户ticket管理页面",notes="用户ticket管理页面")
    public String selectStaffTicket( String staffId, ModelMap mmap)
    {

        Staff staff = staffService.selectStaffById(staffId);
        List<Platform> platforms = platformService.selectStaffPlatform(staffId);
        mmap.put("id",staff.getId());
        if (AssertUtil.isNotEmpty(platforms)){
            String ticket = redisUtilService.getTicket(staff.getId(),platforms.get(0).getPlatformId());
            mmap.put("ticket",ticket);
        }
        if (AssertUtil.isNotEmpty(staff)){
            mmap.put("name",staff.getUsername());
            mmap.put("mobile",staff.getMobile());
        }
        mmap.put("platform",platforms);
        return "sso/staff/ticket";
    }

    @GetMapping("staff/staffIdByRoleList")
    @ApiOperation(value="用户角色列表页面",notes="用户角色列表页面")
    public String staffIdByRoleList(String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return "sso/staff/roleList";
    }

    @GetMapping("staff/staffIdByAreaList")
    @ApiOperation(value="用户园区列表页面",notes="用户园区列表页面")
    public String staffIdByAreaList(String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return "sso/staff/areaList";
    }
    /**
     * 修改用户信息
     */
    @GetMapping("staff/edit/{id}")
    public String editStaff(@PathVariable("id") String id, ModelMap mmap)
    {
        Staff staff = staffService.selectStaffById(id);
        AreaStaff areaStaff = new AreaStaff();
        areaStaff.setStaffId(staff.getId());
        areaStaff.setAreaId(staff.getAreaId());
        List<AreaStaff> list = areaStaffService.selectAreaStaffList(areaStaff);
        if (list.size()>0){
            staff.setStatus(Long.valueOf(list.get(0).getStatus()));
        }
        mmap.put("staff", staff);
        return  "sso/staff/edit";
    }

//---------------------------------------staff-----------------------------------------------------------------------------------
//---------------------------------------分割线-----------------------------------------------------------------------------------
//---------------------------------------menu-----------------------------------------------------------------------------------


    @GetMapping("menu/add")
    @ApiOperation(value="菜单新增页面",notes="菜单新增页面")
    public String add( String parentId,String platformId, ModelMap mmap)
    {
        Menu menu = null;
        if (!StringUtils.equals("0",parentId))
        {
            menu = menuService.selectMenuByMenuId(parentId);
        }
        else
        {
            menu = new Menu();
            menu.setMenuId("0");
            menu.setPlatformId(platformId);
            List<Menu>  list = menuService.selectMenuList(menu);
            if (list.size()>0){
                menu.setMenuId( list.get(0).getMenuId());
            }
            menu.setMenuName("主目录");
        }

        mmap.put("menu", menu);
        mmap.put("platformId", platformId);
        return "sso/menu/add";
    }
    /**
     * 修改菜单
     */
    @GetMapping("menu/edit/{menuId}")
    @ApiOperation(value="修改菜单页面",notes="修改菜单页面")
    public String edit(@PathVariable("menuId") String menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuByMenuId(menuId));
        return  "sso/menu/edit";
    }

    /**
     * 选择菜单树
     */
    @GetMapping("menu/selectMenuTree/{menuId}")
    @ApiOperation(value="选择菜单树页面",notes="选择菜单树页面")
    public String selectMenuTree(@PathVariable("menuId") String menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuByMenuId(menuId));
        return  "sso/menu/tree";
    }

    @GetMapping("menu/icon")
    @ApiOperation(value="选择菜单图标页面",notes="选择菜单图标页面")
    public String icon()
    {
        return   "sso/menu/icon";
    }

    @GetMapping("menu/menu")
    @ApiOperation(value="菜单页面",notes="菜单页面")
    public String menu(ModelMap mmap, @ApiParam(value = "系统ID") @RequestParam String platformId, HttpServletRequest request, HttpServletResponse response)
    {
        mmap.put("platformId",platformId);

        return "sso/menu/menu";
    }

//---------------------------------------menu-----------------------------------------------------------------------------------
//---------------------------------------分割线-----------------------------------------------------------------------------------
//---------------------------------------platform-----------------------------------------------------------------------------------


    @GetMapping("platform/platform")
    @ApiOperation(value="系统列表页面",notes="系统列表页面")
    public String platform(ModelMap mmap)
    {
        List<Platform> platformList =platformService.selectPlatformList(new Platform());
        mmap.put("platformList",platformList);
        return "sso/platform/platform";
    }

    @GetMapping("platform/platformList")
    @ApiOperation(value="系统列表页面",notes="系统列表页面")
    public String platformList(ModelMap mmap)
    {
        List<Platform> platformList =platformService.selectPlatformList(new Platform());
        mmap.put("platformList",platformList);
        return "sso/platform/platformList";
    }


    @GetMapping("platform/add")
    @ApiOperation(value="新增系统页面",notes="新增系统页面")
    public String addPlatform()
    {
        return "sso/platform/add";
    }


    @GetMapping("platform/edit/{platformId}")
    @ApiOperation(value="修改系统页面",notes="修改系统页面")
    public String editPlatform(@PathVariable("platformId") String platformId, ModelMap mmap)
    {
        Platform platform = platformService.selectPlatformByPlatformId(platformId);
        mmap.put("platform", platform);
        return "sso/platform/edit";
    }

}
